package com.library.uiframework.widget.icon;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.library.uiframework.R;
import com.library.uiframework.utils.DisplayUtils;

/**
 * @author hongkai.qian
 * @version 1.0.0
 * @since 15-9-25
 */
public class IconTextTextView extends LinearLayout {

    private IconTextView mIconTextView;
    private TextView mTvTextView;

    private static final int[] ATTRS = new int[]{
            android.R.attr.gravity
    };

    public IconTextTextView(Context context) {
        super(context);
        init(context, null, 0);
    }

    public IconTextTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public IconTextTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public IconTextTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs, defStyleAttr);
    }

    private void init(Context context, AttributeSet attrs, int defStyle) {

        mIconTextView = new IconTextView(context);
        mTvTextView = new TextView(context);
        mTvTextView.setGravity(Gravity.CENTER);
        mTvTextView.setSingleLine();

        ColorStateList attrTextColor = null;
        ColorStateList attrPureTextColor = null;
        int attrTextSize = DisplayUtils.dp2px(14);
        int attrPureTextSize = 0;
        int mBkgTextColor;

        int attrPadding = -1;

        int order = 0;

        int verticalMargin = 0;
        int horizontalMargin = 0;

        if (attrs != null) {
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.IconIconTextView, defStyle, 0);
            for (int i = (a != null ? (a.getIndexCount() - 1) : -1); i >= 0; i--) {
                int attr = a.getIndex(i);
                if (attr == R.styleable.IconIconTextView_icon_text) {
                    CharSequence arrtText = a.getText(attr);
                    if (arrtText != null) {
                        mIconTextView.setText(arrtText.toString());
                    }
                } else if (attr == R.styleable.IconIconTextView_icon_textColor) {
                    attrTextColor = a.getColorStateList(attr);
                } else if (attr == R.styleable.IconIconTextView_icon_textSize) {
                    attrTextSize = a.getDimensionPixelSize(attr, attrTextSize);
                    mIconTextView.setFixedTextSize(true);
                } else if (attr == R.styleable.IconIconTextView_icon_pureTextSize) {
                    attrPureTextSize = a.getDimensionPixelSize(attr, attrPureTextSize);
                } else if (attr == R.styleable.IconIconTextView_icon_bkgText) {
                    String bkgText = a.getText(attr).toString();
                    if (!TextUtils.isEmpty(bkgText)) {
                        mIconTextView.setBkgText(bkgText);
                    }
                } else if (attr == R.styleable.IconIconTextView_icon_bkgTextColor) {
                    mBkgTextColor = a.getColorStateList(attr).getDefaultColor();
                    mIconTextView.setBkgTextColor(mBkgTextColor);
                } else if (attr == R.styleable.IconIconTextView_icon_pureText) {
                    CharSequence arrtText = a.getText(attr);
                    if (arrtText != null) {
                        mTvTextView.setText(arrtText);
                    }
                } else if (attr == R.styleable.IconIconTextView_icon_order) {
                    order = a.getInt(attr, order);
                } else if (attr == R.styleable.IconIconTextView_icon_padding) {
                    attrPadding = a.getDimensionPixelSize(attr, attrPadding);
                } else if (attr == R.styleable.IconIconTextView_icon_pureTextColor) {
                    attrPureTextColor = a.getColorStateList(attr);
                } else if (attr == R.styleable.IconIconTextView_icon_marginVertical) {
                    verticalMargin = a.getDimensionPixelSize(attr, verticalMargin);
                } else if (attr == R.styleable.IconIconTextView_icon_marginHorizontal) {
                    horizontalMargin = a.getDimensionPixelSize(attr, horizontalMargin);
                }
            }
            if (a != null) {
                a.recycle();
            }
        }

        TypedArray array = context.obtainStyledAttributes(attrs, ATTRS);
        setGravity(array.getInt(0, Gravity.CENTER));

        int orientation = getOrientation();
        int padding = attrPadding >= 0 ? attrPadding : attrTextSize >> 2;
        if (order == 0) {
            addView(mTvTextView, makeLayoutParamsWithMargin(horizontalMargin, verticalMargin, orientation, true));
            addView(mIconTextView, makeLayoutParamsWithMargin(horizontalMargin, verticalMargin, orientation, false));
            if (orientation == HORIZONTAL) {
                mTvTextView.setPadding(0, 0, padding, 0);
            } else {
                mTvTextView.setPadding(0, 0, 0, padding);
            }
        } else {
            addView(mIconTextView, makeLayoutParamsWithMargin(horizontalMargin, verticalMargin, orientation, true));
            addView(mTvTextView, makeLayoutParamsWithMargin(horizontalMargin, verticalMargin, orientation, false));
            if (orientation == HORIZONTAL) {
                mTvTextView.setPadding(padding, 0, 0, 0);
            } else {
                mTvTextView.setPadding(0, padding, 0, 0);
            }
        }

        final ColorStateList iconTextColor = attrTextColor != null ? attrTextColor : ColorStateList.valueOf(Color.MAGENTA);
        mIconTextView.setTextColor(iconTextColor);
        mIconTextView.setTextSize(attrTextSize);
        mTvTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, attrPureTextSize > 0 ? attrPureTextSize : attrTextSize);
        mTvTextView.setTextColor(attrPureTextColor != null ? attrPureTextColor : iconTextColor);
    }

    private LayoutParams makeLayoutParamsWithMargin(int horizontalMargin, int verticalMargin, int orientation, boolean start) {
        LayoutParams layoutParams = generateDefaultLayoutParams();
        layoutParams.leftMargin = orientation == HORIZONTAL && !start ? 0 : horizontalMargin;
        layoutParams.rightMargin = orientation == HORIZONTAL && start ? 0 : horizontalMargin;
        layoutParams.topMargin = orientation == VERTICAL && !start ? 0 : verticalMargin;
        layoutParams.bottomMargin = orientation == VERTICAL && start ? 0 : verticalMargin;
        return layoutParams;
    }

    public void setTextColor(int color) {
        mTvTextView.setTextColor(color);
    }

    public void setIconTextColor(int color) {
        mIconTextView.setTextColor(color);
    }

    /**
     * @param size 文字大小, sp
     */
    public void setTextSize(int size) {
        mTvTextView.setTextSize(size);
        mIconTextView.setTextSize(DisplayUtils.dp2px(size));
    }

    public IconTextView getIconTextView() {
        return mIconTextView;
    }

    public TextView getTextView() {
        return mTvTextView;
    }

    public void setIconAndText(int resId, String text) {
        setIconText(resId);
        setText(text);
    }

    public void setIconAndText(int resId, int resTextId) {
        setIconText(resId);
        setText(resTextId);
    }

    public void setIconText(int resId) {
        mIconTextView.setText(resId);
    }

    public void setText(CharSequence text) {
        mTvTextView.setText(text);
    }

    public void setText(int txtResId) {
        mTvTextView.setText(txtResId);
    }

    public void setIconVisibility(int visibility) {
        mIconTextView.setVisibility(visibility);
    }
}
